//接口地址
var url="http://47.105.48.86:8081/ad-proxy";

//	获取地址栏参数
function GetQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}

//	错误提示
function isErr(res) {
	layer.msg(res.msg);
	if(res.code == "5"){
		location.href="login.html";
	}
}
//	获取token
var token=GetQueryString("token");
if(token == "" || token == null || token == undefined){
	location.href='login.html';
}
var token_id=GetQueryString("id");
var token_str="?token="+token+"&id="+token_id;
$(".href_url").on("click",function(){
	location.href=$(this).attr("data")+token_str;
})

//个人信息获取
$(".username_type1").html(localStorage.getItem("username_type1"));
$(".name_type1").html(localStorage.getItem("name_type1"));

function logout(){
	$.ajax({
		url: url + "/sys/logout",
		type: 'Get',
		//		参数需转为字符串
		//		json跨域
		dataType: 'json',
		//		contentType类型为json
		contentType: "application/json",
		useDefaultXhrHeader: false,
		//		请求头加上X-Token 
		 beforeSend: function(XMLHttpRequest) {
			//		token 为页面所获取
			   
			XMLHttpRequest.setRequestHeader("X-Token", token); 
		},
		success: function(result) {
			if(result.code == '0') {
				layer.msg('退出成功',{time:2*1000},function() {
					location.href='login.html';
				});
			} else {
				isErr(result);
			}
		},
		err: function(err) {
			layer.msg("请求失败");
		}
	});
}

function resetPWD(){
    var oldPasswd=$(".oldPasswd").val();
    var newPasswd=$(".newPasswd").val();
    var newPasswd2=$(".newPasswd2").val();
    if(!oldPasswd){
    	layer.msg("请输入旧密码");
    	return false;
    }
    if(!newPasswd){
    	layer.msg("请输入新密码");
    	return false;
    }
    if(!newPasswd2){
    	layer.msg("请再次输入密码");
    	return false;
    }
    if(newPasswd != newPasswd2){
    	layer.msg("两次密码输入不一致");
    	return false;
    }
    var d={};
    d.oldPassword=oldPasswd;
    d.newPassword=newPasswd;
    $.ajax({
		url: url + "/sys/password",
		type: 'POST',
		data: JSON.stringify(d),
		dataType: 'json',
		contentType: "application/json",
		
		beforeSend: function(XMLHttpRequest) {
//			XMLHttpRequest.setRequestHeader("X-Token", token); 
		},
		useDefaultXhrHeader: false,
		success: function(result) {
			if(result.code == '0') {
				layer.msg(result.msg,{time:2*1000},function() {
					$("#pwdModal").modal("hide");
					$(".oldPasswd").val("");
				    $(".newPasswd").val("");
				    $(".newPasswd2").val("");
				});
			} else {
				isErr(result);
			}
		},
		err: function(err) {
			layer.msg("请求失败");
		}
	});
}


//title弹框
function tips(event,str) {
    layer.tips(str, $(event.target), {
        time: 0,
        tips: [1, '#fffee3']
    });
}


//显示用户账户余额和可转余额信息接口

$.ajax({
	url: url + "/sys/account",
	type: 'POST',
	//		参数需转为字符串
//	data: JSON.stringify(j),
	//		json跨域
	dataType: 'json',
	//		contentType类型为json
	contentType: "application/json",

	//		请求头加上X-Token 
	 beforeSend: function(XMLHttpRequest) {
		//		token 为页面所获取
		   
		XMLHttpRequest.setRequestHeader("X-Token", token); 
	},
	useDefaultXhrHeader: false,
	success: function(result) {
		if(result.code == '0') {
			$(".amount").html(result.data.amount);
			$(".actAmount").html(result.data.actAmount);
		} else {
			isErr(result);
		}
	},
	err: function(err) {
		layer.msg("请求失败");
	}
	
});

$(document).on("click",".close-layer",function () {
   layer.closeAll();
});



//分页方法
function paging(page,pageSize,total){
	page = Number(page);
	
	//最大页数
	var maxPage = Math.ceil(total/pageSize) || 1;
	if(page>maxPage){
		//取值不能超过maxPage
	    page = Math.min(page,maxPage);
	    tiaozhuan(page);
	}else if(page<1){
		//取值不能小于1
	    page = Math.max(page,1);
	    tiaozhuan(page);
	}else{
		//	当前页数
	$('.ui-paging-count').val(page);
	//首页的置灰处理
	$('.js-page-first').removeClass('ui-pager-disabled');
	//上一页
	$('.js-page-prev').removeClass('ui-pager-disabled').attr("data-page",page-1);
	//下一页
	$('.js-page-next').removeClass('ui-pager-disabled').attr("data-page",page+1);
	//最末页
	$('.js-page-last').removeClass('ui-pager-disabled').attr("data-page",maxPage);
	//总页数显示
	$('.js-page-total').text(maxPage);
	
	
	//当前显示多少页的处理
	$(".ui-select-pagesize").val(pageSize);
	//第一页的处理
	if(page == 1){
		$(".js-page-first").addClass('ui-pager-disabled').attr("data-page","");
		$(".js-page-prev").addClass('ui-pager-disabled').attr("data-page","");
	}
	if(page == maxPage){
		//最后一页的处理
		$(".js-page-last").addClass('ui-pager-disabled').attr("data-page","");
		$(".js-page-next").addClass('ui-pager-disabled').attr("data-page","");
	}
	}
	 
	
};
//下拉选择分页条数的操作
$('.ui-select-pagesize').on("change",function(){
	pageSize = $(this).val();	
	tiaozhuan(p);
});

//跳转到X页  blur
$(".ui-paging-count").on("blur",function(){
	p = $(this).val();
	tiaozhuan(p);
});

//上一页，下一页，首页，最末页的跳转
$('.ui-pager').on("click",function(){
	p = $(this).attr("data-page");
	tiaozhuan(p);
});


//下载excel文件
function exportCsv(obj){
    //title ["","",""]
    var title = obj.title;
    //titleForKey ["","",""]
    var titleForKey = obj.titleForKey;
    var data = obj.data;
    var str = [];
    str.push(obj.title.join(",")+"\n");
    for(var i=0;i<data.length;i++){
        var temp = [];
        for(var j=0;j<titleForKey.length;j++){
            temp.push(data[i][titleForKey[j]]);
    }
    str.push(temp.join(",")+"\n");
    }
    var uri = 'data:text/csv;charset=utf-8,' + encodeURIComponent(str.join(""));  
    var downloadLink = document.createElement("a");
    downloadLink.href = uri;
    downloadLink.download = "export.csv"; 
    document.body.appendChild(downloadLink);
    downloadLink.click();
    document.body.removeChild(downloadLink); 
}

//备用的ajax2.
function getAgentMsg2(param2,method2,datas2){
	$.ajax({
		url: url + param2,
		type: method2,
		dataType: 'json',
		data:JSON.stringify(datas2),
		async:false,
		contentType: "application/json",
		useDefaultXhrHeader: false,
		 beforeSend: function(XMLHttpRequest) {
			XMLHttpRequest.setRequestHeader("X-Token", token); 
		},
		success: function(result) {
//		console.log(result);
			if(result.code == '0') {
				dataResult2 = result;
//				layer.msg('退出成功',{time:2*1000},function() {
//					location.href='login.html';
//				});
			} else {
				isErr(result);
			}
		},
		err: function(err) {
			layer.msg("请求失败");
		}
	});
}